//
//  ViewController.m
//  CAShapeLayer
//
//  Created by 楼楚 on 16/3/2.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) CAShapeLayer *shape1;
@property (nonatomic,strong) CAShapeLayer *shape2;

@property (nonatomic,strong) CAShapeLayer *shape3;

@property (nonatomic,strong) NSTimer      *timer;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 200)];
    
    UIBezierPath *rect = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
    
    UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
    
   _shape1 = [CAShapeLayer layer];
    _shape1.frame = CGRectMake(10, 10, 200, 200);
    
    _shape1.borderWidth = 3.f;
    // 禁止内容显示超出CAShapeLayer的frame值
    _shape1.masksToBounds = NO;
    
    _shape1.fillColor = [UIColor clearColor].CGColor;
    _shape1.strokeColor = [UIColor redColor].CGColor;
    _shape1.lineWidth = 1.0f;
    _shape1.strokeStart = 0.f;
    _shape1.strokeEnd = 0.75;
    _shape1.path = oval.CGPath;
    
    
    _shape2 = [CAShapeLayer layer];
    _shape2.frame = CGRectMake(10,300, 200, 200);
    
    _shape2.borderWidth = 3.f;
    // 禁止内容显示超出CAShapeLayer的frame值
    _shape2.masksToBounds = NO;
    
    _shape2.fillColor = [UIColor clearColor].CGColor;
    _shape2.strokeColor = [UIColor greenColor].CGColor;
    _shape2.lineWidth = 2.0f;
    _shape2.strokeStart = 0.f;
    _shape2.strokeEnd = 0.5;
    
    _shape2.path = rect.CGPath;
    
    _shape3 = [CAShapeLayer layer];
    _shape3.frame = CGRectMake(210, 10, 200, 200);
    
    _shape3.borderWidth = 3.f;
    // 禁止内容显示超出CAShapeLayer的frame值
    _shape3.masksToBounds = NO;
    
    _shape3.fillColor = [UIColor clearColor].CGColor;
    _shape3.strokeColor = [UIColor blueColor].CGColor;
    _shape3.lineWidth = 3.f;
    _shape3.strokeStart = 0.f;
    _shape3.strokeEnd = 0.25;
    _shape3.path = circle.CGPath;
    
    [self.view.layer addSublayer:_shape1];
    [self.view.layer addSublayer:_shape2];
    [self.view.layer addSublayer:_shape3];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(animationEvent) userInfo:nil repeats:YES];
    
   
//    _shapeLayer = [CAShapeLayer layer];
//    _shapeLayer.frame = (CGRect){CGPointMake(0, 0), CGSizeMake(200, 200)};
//    _shapeLayer.position = self.view.center;
//    _shapeLayer.path = [self getStar1BezierPath].CGPath;
//    _shapeLayer.fillColor     = [UIColor clearColor].CGColor;
//    _shapeLayer.strokeColor   = [UIColor redColor].CGColor;
//    _shapeLayer.lineWidth     = 2.f;
//    [self.view.layer addSublayer:_shapeLayer];
//    
//    // 创建定时器
//    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f
//                                              target:self
//                                            selector:@selector(pathAnimation)
//                                            userInfo:nil
//                                             repeats:YES];
}

- (void)animationEvent
{
    _shape1.strokeEnd = arc4random() %100/100.f;
    _shape2.strokeEnd = arc4random() %100/100.f;
    _shape3.strokeEnd = arc4random() %100/100.f;
}
//-(void) pathAnimation{
//    static int i = 0;
//    if (i++ % 2 == 0) {
//        CABasicAnimation *circleAnim = [CABasicAnimation animationWithKeyPath:@"path"];
//        circleAnim.removedOnCompletion = NO;
//        circleAnim.duration            = 1;
//        circleAnim.fromValue           = (__bridge id)[self getStar1BezierPath].CGPath;
//        circleAnim.toValue             = (__bridge id)[self getStar2BezierPath].CGPath;
//        _shapeLayer.path               = [self getStar2BezierPath].CGPath;
//        [_shapeLayer addAnimation:circleAnim forKey:@"animateCirclePath"];
//    } else {
//        CABasicAnimation *circleAnim = [CABasicAnimation animationWithKeyPath:@"path"];
//        circleAnim.removedOnCompletion = NO;
//        circleAnim.duration            = 1;
//        circleAnim.fromValue           = (__bridge id)[self getStar2BezierPath].CGPath;
//        circleAnim.toValue             = (__bridge id)[self getStar1BezierPath].CGPath;
//        _shapeLayer.path               = [self getStar1BezierPath].CGPath;
//        [_shapeLayer addAnimation:circleAnim forKey:@"animateCirclePath"];
//    }
//
//}
///**
// *  贝塞尔曲线1
// *
// *  @return 贝塞尔曲线
// */
//-(UIBezierPath *)getStar1BezierPath {
//    //// Star Drawing
//    UIBezierPath* starPath = [UIBezierPath bezierPath];
//    [starPath moveToPoint: CGPointMake(22.5, 2.5)];
//    [starPath addLineToPoint: CGPointMake(28.32, 14.49)];
//    [starPath addLineToPoint: CGPointMake(41.52, 16.32)];
//    [starPath addLineToPoint: CGPointMake(31.92, 25.56)];
//    [starPath addLineToPoint: CGPointMake(34.26, 38.68)];
//    [starPath addLineToPoint: CGPointMake(22.5, 32.4)];
//    [starPath addLineToPoint: CGPointMake(10.74, 38.68)];
//    [starPath addLineToPoint: CGPointMake(13.08, 25.56)];
//    [starPath addLineToPoint: CGPointMake(3.48, 16.32)];
//    [starPath addLineToPoint: CGPointMake(16.68, 14.49)];
//    [starPath closePath];
//    
//    return starPath;
//}
//
///**
// *  贝塞尔曲线2
// *
// *  @return 贝塞尔曲线
// */
//-(UIBezierPath *)getStar2BezierPath {
//    //// Star Drawing
//    UIBezierPath* starPath = [UIBezierPath bezierPath];
//    [starPath moveToPoint: CGPointMake(22.5, 2.5)];
//    [starPath addLineToPoint: CGPointMake(32.15, 9.21)];
//    [starPath addLineToPoint: CGPointMake(41.52, 16.32)];
//    [starPath addLineToPoint: CGPointMake(38.12, 27.57)];
//    [starPath addLineToPoint: CGPointMake(34.26, 38.68)];
//    [starPath addLineToPoint: CGPointMake(22.5, 38.92)];
//    [starPath addLineToPoint: CGPointMake(10.74, 38.68)];
//    [starPath addLineToPoint: CGPointMake(6.88, 27.57)];
//    [starPath addLineToPoint: CGPointMake(3.48, 16.32)];
//    [starPath addLineToPoint: CGPointMake(12.85, 9.21)];
//    [starPath closePath];
//    
//    return starPath;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
